(function() {

		angular.module('cf-content').directive('citymanage', ['$timeout', "$window", function($timeout, $window) {
			return {
				restrict: 'AE',
				templateUrl: 'app/page/citymanage/citymanage.html',
				// replace:true,
				controller: citymanage,
				controllerAs: 'cityCtrl'
			}
		}])

		citymanage.$inject = ['$scope','$uibModal', 'Api']

		function citymanage($scope,$uibModal, Api) {
			var vm = this;
			//model
			vm.EnsureModel = function openModel(size, item, callback) {
				var modalInstance = $uibModal.open({
					animation: true,
					templateUrl: 'EnsureModel',
					controller: 'EnsureModelCtrl',
					size: size,
					resolve: {
						item: function() {
							return '删除'+item;
						}
					}
				});

				modalInstance.result.then(callback);
			}

			vm.InfoModel = function openModel(size, info,type,callback) {
				var modalInstance = $uibModal.open({
					animation: true,
					templateUrl: 'InfoModel',
					controller: 'InfoModelCtrl',
					size: size,
					resolve: {
						info: function() {
							return info;
						},
						type:function(){
							return type;
						}
					}
				});

				modalInstance.result.then(callback);
			}
			initProvince();
			vm.provinceList = {
				tooltitle: '省份',
				insertdata: {
					id: ''
				},
				selectedId: '',
				addible: false,
				options:[],
				wait: true,
				load: load,
				data: [],
				minus:minus,
				save:save
			}
			vm.cityList = {
				tooltitle: '已开通城市',
				insertdata: {
					id: ''
				},
				wait: true,
				selectedId: '',
				addible: false,
				options:[],
				minus: childminus,
				save: childsave,
				data: []
			}
			function childminus(obj){
				var selectedData;
				for (var i in obj.data) {
					if (obj.data[i].id == obj.selectedId) {
						selectedData = obj.data[i];
						break;
					}
				}
				if(selectedData){
					vm.EnsureModel('sm', selectedData.name, function(data) {
							Api.City.Out({
								city_id:selectedData.id,
							}).$promise.then(function(data) {
								vm.InfoModel('sm','操作成功')
								load();
							})
					})
				}
				else{
					vm.InfoModel('sm','请选择要删除的城市')
				}
			}
			function minus(obj) {
				var selectedData;
				for (var i in obj.data) {
					if (obj.data[i].id == obj.selectedId) {
						selectedData = obj.data[i];
						break;
					}
				}
				if(selectedData){
					vm.EnsureModel('sm', selectedData.name, function(data) {
							Api.Province.Display({
								province_id:selectedData.id,
								display:false
							}).$promise.then(function(data) {
								vm.InfoModel('sm','操作成功')
								initProvince();
							})
					})
				}
				else{
					vm.InfoModel('sm','请选择要删除的省份')
				}
			}


			function save(obj) {
				var selectedData;
				if (obj.addible) {
					if (obj.insertdata.id == '')
						return;
					obj.addible = false;
					Api.Province.Display({
						province_id:obj.insertdata.id,
						display:true
					}).$promise.then(function(data) {
						if(data.code==0){
							vm.InfoModel('sm','添加成功','success')
						}
						else{
							vm.InfoModel('sm','添加失败','error')
						}
						initProvince();
					})
				}
				else 
					return;
			}

			function childsave(obj) {
				var selectedData;
				if (obj.addible) {
					if (obj.insertdata.id == '')
						return;
					if(!obj.insertdata.id){
						vm.InfoModel('sm','请先选择省份')
					}
					obj.addible = false;
					Api.City.In({
						city_id: obj.insertdata.id,
					}).$promise.then(function(data) {
						if(data.code==0){
							vm.InfoModel('sm','添加成功','success')
						}
						else{
							vm.InfoModel('sm','添加失败','error')
						}
						load();
					})
				}
			}

			function load() {
				initCity(vm.provinceList.selectedId)
			}

			


			function initProvince(){
				Api.Area.ProvinceQuery({shown:true}).$promise.then(function(data){
					if(!vm.provinceList.selectedId)
						vm.provinceList.selectedId=data.data[0].id
					vm.provinceList.data=data.data;
					initCity(vm.provinceList.selectedId)
					initUnshowProvince()
				})
			}
			function initUnshowProvince(){
				Api.Area.ProvinceQuery({shown:false}).$promise.then(function(data){
					vm.provinceList.options=data.data;
				})
			}
			function initCity(id){
				Api.Area.CityQuery({type:1,province_id:id}).$promise.then(function(data){
					if(data.code!=0)
						vm.InfoModel('sm','加载失败','error')
					if(!vm.cityList.selectedId)
						vm.cityList.selectedId=data.data[0].id
					vm.cityList.data=data.data.sort(function(a,b){return a.id-b.id});
					initUnshowCoty(id)
				})
			}
			function initUnshowCoty(id){
				Api.Area.CityQuery({type:2,province_id:id}).$promise.then(function(data){
					vm.cityList.options=data.data;
					console.log(vm.cityList.options)
				})
			}
		}

})();